package pl.edu.pw.polygen;

import java.util.Iterator;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import pl.edu.pw.polygen.beans.FileBean;
import pl.edu.pw.polygen.modeler.server.EditorServer;
import pl.edu.pw.polygen.ui.explorer.ProjectExplorer;
import pl.edu.pw.polygen.ui.tabsheet.PolygenTabSheet;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.TabSheet.CloseHandler;
import com.vaadin.ui.TabSheet.Tab;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@Scope("prototype")
@Component
public class MainWindow extends Window{

	private static final long serialVersionUID = 7407282214258571536L;
	
	private Logger LOG = Logger.getLogger(MainWindow.class);
	
	@Autowired
	private HeaderPanel headerPanel;
	
	@Autowired
	private ProjectExplorer projectExplorer;
	
	private EditorServer editor;
	
	private PolygenTabSheet contentTabSheet;


	public MainWindow() {
		super();
	}

	@PostConstruct
	protected void init() {
		getTabSheet();
		VerticalLayout windowLayout = new VerticalLayout();
		setContent(windowLayout);
		windowLayout.setSizeFull();
		setCaption("web-polygen-0.0.1");
		windowLayout.addComponent(headerPanel);
		
		final HorizontalSplitPanel mainSplit = new HorizontalSplitPanel();
		mainSplit.setSizeFull();
		mainSplit.setStyleName("main-split");
		windowLayout.addComponent(mainSplit);
		windowLayout.setExpandRatio(mainSplit, 1);
		mainSplit.setFirstComponent((VerticalLayout)projectExplorer);
		mainSplit.setSecondComponent(contentTabSheet);
		mainSplit.setSplitPosition(20, Sizeable.UNITS_PERCENTAGE);
		mainSplit.setLocked(true);
		
		addListener(new ResizeListener() {
			
			private static final long serialVersionUID = -7735844786116705377L;

			@Override
			public void windowResized(ResizeEvent e) {
			/*	LOG.debug("ResizeEvent catched:");
				int height = (int) ApplicationHelper.getCurrentApplication()
				.getMainWindow().getHeight();
				int width = (int) ApplicationHelper.getCurrentApplication()
				.getMainWindow().getWidth();
				LOG.debug("Main application size: " + width + " " + height);
				int newWidth =  (int) (width - width*0.25 - 70 - 160);
				int newHeight = (int)(height - 23 - 32 - 44 );
				LOG.debug("Editor new size: " + newWidth + " " + newHeight);
				editor.setNewSize(newWidth, newHeight);*/
			}
		});
	}
	
	private TabSheet getTabSheet(){
		contentTabSheet =new PolygenTabSheet();
		contentTabSheet.setCloseHandler(new CloseHandler() {
			
			@Override
			public void onTabClose(TabSheet tabsheet, com.vaadin.ui.Component tabContent) {
				tabsheet.removeComponent(tabContent);
			}
		});
		return contentTabSheet;
	}
	
	public Tab addTab(FileBean fileBean,Panel panel){
		VerticalLayout verticalLayout = (VerticalLayout) panel.getContent();
		EditorServer editorServer = (EditorServer) verticalLayout.getComponent(0);
		editorServer.uploadData(fileBean.getContent());
		Tab tab = contentTabSheet.addTab(panel, fileBean);
		tab.setClosable(true);
		return tab;
	}
	
	public void loadProjects(){
		projectExplorer.loadProjects();
	}
	
	public EditorServer getSelectedEditorServer(){
		Tab tab = contentTabSheet.getTab(contentTabSheet.getSelectedTab());
		Panel panel = (Panel)tab.getComponent();
		VerticalLayout verticalLayout = (VerticalLayout) panel.getContent();
		EditorServer editorServer = (EditorServer) verticalLayout.getComponent(0);
		return editorServer;
	}
	
	public FileBean getSelectedTab(){
		Tab tab = contentTabSheet.getTab(contentTabSheet.getSelectedTab());
		return contentTabSheet.getFileBean(tab.getCaption());
	}
	
	public boolean isOpen(String caption){
		Iterator<com.vaadin.ui.Component> iterator = contentTabSheet.getComponentIterator();
		while(iterator.hasNext()){
			com.vaadin.ui.Component component= iterator.next();
			if(caption.equals(contentTabSheet.getTabCaption(component)))
				return true;
		}
		return false;
	}

	public EditorServer getEditorServer(String caption) {
		Iterator<com.vaadin.ui.Component> iterator = contentTabSheet.getComponentIterator();
		while(iterator.hasNext()){
			com.vaadin.ui.Component component= iterator.next();
			if(caption.equals(contentTabSheet.getTabCaption(component))){
				Panel panel = (Panel)component;
				VerticalLayout verticalLayout = (VerticalLayout) panel.getContent();
				EditorServer editorServer = (EditorServer) verticalLayout.getComponent(0);
				return editorServer;
			}
		}
		return null;
	}
	
}
